//
//  CalendarView.m
//  ZhangBen
//
//  Created by tinyfool on 08-10-26.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "TdCalendarView.h"
#import <QuartzCore/QuartzCore.h>

const float headHeight=15;
const float itemHeight=35;
const float prevNextButtonSize=20;
const float prevNextButtonSpaceWidth=15;
const float prevNextButtonSpaceHeight=12;
const float titleFontSize=30;
const int	weekFontSize=12;

@implementation TdCalendarView

@synthesize currentMonthDate;
@synthesize currentSelectDate;
@synthesize currentTime;
@synthesize viewImageView;
@synthesize calendarViewDelegate,monthFlagArray;
-(void)getSchedule{
    int dayCount=[self getDayCountOfaMonth:currentMonthDate];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Chongqing"]];
    NSString* strDate1=[NSString stringWithFormat:@"%4d-%2d-%2d 00:00:01",currentMonthDate.year,currentMonthDate.month,1];
    NSString* strDate2=[NSString stringWithFormat:@"%4d-%2d-%2d 23:59:59",currentMonthDate.year,currentMonthDate.month,dayCount];
    NSDate *date1 = [dateFormatter dateFromString:strDate1];
    NSDate *date2 = [dateFormatter dateFromString:strDate2];
    date1 = [[NSDate alloc] initWithTimeInterval:(8 * 60 * 60) sinceDate:date1];
    date2 = [[NSDate alloc] initWithTimeInterval:(8 * 60 * 60) sinceDate:date2];
    [dateFormatter release];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSDictionary * ret = [[MyClass getInstance] getSchedule:date1 toTime:date2 ];
    if ([[ret objectForKey:@"code"] intValue]==0) {
        if ([ret objectForKey:@"size"] > 0) {
            self.monthFlagArray = [ret objectForKey:@"list"];
        }
    }
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
-(void)initCalView{
	currentTime=CFAbsoluteTimeGetCurrent();
	currentMonthDate=CFAbsoluteTimeGetGregorianDate(currentTime,CFTimeZoneCopyDefault());
	currentMonthDate.day=1;
	currentSelectDate = [MyClass getInstance].selectDate;
	[self clearAllDayFlag];	
}

- (id)initWithCoder:(NSCoder *)coder {
    if (self = [super initWithCoder:coder]) {
		[self initCalView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
	
	if (self = [super initWithFrame:frame]) {
		[self initCalView];
        [self getSchedule];
	}
	return self;
}

-(int)getDayCountOfaMonth:(CFGregorianDate)date{
	switch (date.month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			
		case 2:
			if((date.year % 4==0 && date.year % 100!=0) || date.year % 400==0)
				return 29;
			else
				return 28;
		case 4:
		case 6:
		case 9:		
		case 11:
			return 30;
		default:
			return 31;
	}
}


-(NSMutableArray*)getDayFlag:(NSString*)day
{
    return ([self.monthFlagArray objectForKey:day]);

}
-(void)clearAllDayFlag
{
    self.monthFlagArray = nil;
}
-(void)setDayFlag:(NSString*)day flag:(NSMutableArray*)flag
{
    if([flag count]>0)
        [self.monthFlagArray setObject:flag forKey:day];
	
}


-(void)drawTopBarWords{
	int width=self.frame.size.width;
	int s_width=width/7;
	[[UIColor whiteColor] set];
    int fontsize = 0;	
	UIFont *weekfont=[UIFont boldSystemFontOfSize:weekFontSize];
	[[UIColor blackColor] set];
	[@"周一" drawAtPoint:CGPointMake(s_width*0+9,fontsize) withFont:weekfont];
	[@"周二" drawAtPoint:CGPointMake(s_width*1+9,fontsize) withFont:weekfont];
	[@"周三" drawAtPoint:CGPointMake(s_width*2+9,fontsize) withFont:weekfont];
	[@"周四" drawAtPoint:CGPointMake(s_width*3+9,fontsize) withFont:weekfont];
	[@"周五" drawAtPoint:CGPointMake(s_width*4+9,fontsize) withFont:weekfont];
	[[UIColor redColor] set];
	[@"周六" drawAtPoint:CGPointMake(s_width*5+9,fontsize) withFont:weekfont];
	[@"周日" drawAtPoint:CGPointMake(s_width*6+9,fontsize) withFont:weekfont];
	[[UIColor blackColor] set];	
}
-(NSInteger)getHeight{
    float height;
	int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1;
	height=row_Count*itemHeight+headHeight;
    return height;
}
-(void)drawGirdLines{
	
	CGContextRef ctx=UIGraphicsGetCurrentContext();
	int width=self.frame.size.width;
	int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1;
	int s_width=width/7;
	int tabHeight=row_Count*itemHeight+headHeight;
	CGContextSetGrayStrokeColor(ctx,0,1);
	CGContextMoveToPoint	(ctx,0,headHeight);
	CGContextAddLineToPoint	(ctx,0,tabHeight);
	CGContextStrokePath		(ctx);
	CGContextMoveToPoint	(ctx,width,headHeight);
	CGContextAddLineToPoint	(ctx,width,tabHeight);
	CGContextStrokePath		(ctx);
	
	for(int i=1;i<7;i++){
		CGContextSetGrayStrokeColor(ctx,1,1);
		CGContextMoveToPoint(ctx, i*s_width-1, headHeight);
		CGContextAddLineToPoint( ctx, i*s_width-1,tabHeight);
		CGContextStrokePath(ctx);
	}
	
	for(int i=0;i<row_Count+1;i++){
		CGContextSetGrayStrokeColor(ctx,1,1);
		CGContextMoveToPoint(ctx, 0, i*itemHeight+headHeight+3);
		CGContextAddLineToPoint( ctx, width,i*itemHeight+headHeight+3);
		CGContextStrokePath(ctx);
		
		CGContextSetGrayStrokeColor(ctx,0.3,1);
		CGContextMoveToPoint(ctx, 0, i*itemHeight+headHeight);
		CGContextAddLineToPoint( ctx, width,i*itemHeight+headHeight);
		CGContextStrokePath(ctx);
	}
	for(int i=1;i<7;i++){
		CGContextSetGrayStrokeColor(ctx,0.3,1);
		CGContextMoveToPoint(ctx, i*s_width+2, headHeight);
		CGContextAddLineToPoint( ctx, i*s_width+2,tabHeight);
		CGContextStrokePath(ctx);
	}
}


-(int)getMonthWeekday:(CFGregorianDate)date
{
	CFTimeZoneRef tz = CFTimeZoneCopyDefault();
	CFGregorianDate month_date;
	month_date.year=date.year;
	month_date.month=date.month;
	month_date.day=1;
	month_date.hour=0;
	month_date.minute=0;
	month_date.second=1;
	return (int)CFAbsoluteTimeGetDayOfWeek(CFGregorianDateGetAbsoluteTime(month_date,tz),tz);
}

-(void)drawDateWords{
	//CGContextRef ctx=UIGraphicsGetCurrentContext();
	int width=self.frame.size.width;
	int dayCount=[self getDayCountOfaMonth:currentMonthDate];
	int day=0;
	int x=0;
	int y=0;
	int swidth=width/7;
	int curr_Weekday=[self getMonthWeekday:currentMonthDate];
	UIFont *weekfont=[UIFont boldSystemFontOfSize:12];
    
    NSString* scheduleTypeStr = [MyClass getInstance].scheduleTypeStr;

	for(int i=1;i<dayCount+1;i++)
	{
		day=i+curr_Weekday-2;
		x=day % 7;
		y=day / 7;
		NSString *date=[[[NSString alloc] initWithFormat:@"%2d",i] autorelease];
		[date drawAtPoint:CGPointMake(x*swidth+25,y*itemHeight+headHeight) withFont:weekfont];
        
        NSString* strDate=[NSString stringWithFormat:@"%04d%02d%02d",currentMonthDate.year,currentMonthDate.month,i];
        NSArray* arr;
        arr = [self getDayFlag:strDate];
		if([arr count] > 0)
		{
            BOOL isPink=NO;
            BOOL isBlue=NO;
            BOOL isBrown=NO;
            BOOL isOrange=NO;
            BOOL isGreen=NO;
            NSInteger drawCount=0;
            for(NSDictionary* dict in arr) {
                NSString * type = [dict objectForKey:@"type"];
                if ([MyClass getInstance].scheduleType == EScheduleType_all) 
                {
                    if ([type isEqualToString:@"plan"] && !isPink)
                    {
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        isPink = YES;
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"deed"] && !isBlue)
                    {
                        isBlue=YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"birthday"]&& !isGreen)
                    {
                        isGreen =YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];  
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"payday"]&& !isOrange)
                    {
                        isOrange= YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"activity"]&& !isBrown)
                    {
                        isBrown= YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                }
                else
                {
                    if ([type isEqualToString:scheduleTypeStr]) {
                        if ([type isEqualToString:@"plan"] && !isPink)
                        {
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            isPink = YES;
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"deed"] && !isBlue)
                        {
                            isBlue=YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"birthday"]&& !isGreen)
                        {
                            isGreen =YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];  
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"payday"]&& !isOrange)
                        {
                            isOrange= YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"activity"]&& !isBrown)
                        {
                            isBrown= YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                    }
                }
                
            }
		}        

	}
}


- (void) movePrevNext:(int)isPrev{
	currentSelectDate.year=0;
    [MyClass getInstance].selectDate = currentSelectDate;
	[calendarViewDelegate beforeMonthChange:self willto:currentMonthDate];
	int width=self.frame.size.width;
	int posX;
    BOOL isNoShow=NO;
	if(isPrev==1)
	{
		posX=width;
	}
    else if (isPrev==100) {
        isNoShow=YES;
    }
	else
	{
		posX=-width;
	}
	
	UIImage *viewImage;
	CGSize size = self.bounds.size;
    size.height = size.height;
	UIGraphicsBeginImageContext(size);
	[self.layer renderInContext:UIGraphicsGetCurrentContext()];	
	viewImage= UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
    float height;
	int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1;
	height=row_Count*itemHeight+headHeight;
    if (!isNoShow) {
        if(viewImageView==nil)
        {
            viewImageView=[[UIImageView alloc] initWithImage:viewImage];
            
            //viewImageView.center=self.center;
            viewImageView.frame = CGRectMake(0, 0, 320, height);
            [[self superview] addSubview:viewImageView];
        }
        else
        {
            viewImageView.image=viewImage;
        }
	}
	viewImageView.hidden=NO;
	viewImageView.transform=CGAffineTransformMakeTranslation(0, 0);
	self.hidden=YES;
	[self setNeedsDisplay];
	self.transform=CGAffineTransformMakeTranslation(posX,0);
	
	

	//self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height);
	self.hidden=NO;
    if (!isNoShow) {
        [UIView beginAnimations:nil	context:nil];
        [UIView setAnimationDuration:0.2];
        self.transform=CGAffineTransformMakeTranslation(0,0);
        viewImageView.transform=CGAffineTransformMakeTranslation(-posX, 0);
        [UIView commitAnimations];
    }
    else 
    {
        self.transform=CGAffineTransformMakeTranslation(0,0);
        viewImageView.transform=CGAffineTransformMakeTranslation(-posX, 0);
    }
	[calendarViewDelegate monthChanged:currentMonthDate viewLeftTop:self.frame.origin height:height];
    
	[self getSchedule];
}
- (void)movePrevMonth{
	if(currentMonthDate.month>1)
		currentMonthDate.month-=1;
	else
	{
		currentMonthDate.month=12;
		currentMonthDate.year-=1;
	}
	[self movePrevNext:0];
}
- (void)moveNextMonth{
	if(currentMonthDate.month<12)
		currentMonthDate.month+=1;
	else
	{
		currentMonthDate.month=1;
		currentMonthDate.year+=1;
	}
	[self movePrevNext:1];	
}
- (void) drawToday{
	int x;
	int y;
	int day;
	CFGregorianDate today=CFAbsoluteTimeGetGregorianDate(currentTime, CFTimeZoneCopyDefault());
	if(today.month==currentMonthDate.month && today.year==currentMonthDate.year)
	{
		int width=self.frame.size.width;
		int swidth=width/7;
		int weekday=[self getMonthWeekday:currentMonthDate];
		day=today.day+weekday-2;
		x=day%7;
		y=day/7;
		CGContextRef ctx=UIGraphicsGetCurrentContext(); 
//		CGContextSetRGBFillColor(ctx, 0.5, 0.5, 0.5, 1);
        CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
		CGContextMoveToPoint(ctx, x*swidth+6, y*itemHeight+headHeight+2);
		CGContextAddLineToPoint(ctx, x*swidth+swidth, y*itemHeight+headHeight+2);
		CGContextAddLineToPoint(ctx, x*swidth+swidth, y*itemHeight+headHeight+itemHeight-2);
		CGContextAddLineToPoint(ctx, x*swidth+6, y*itemHeight+headHeight+itemHeight-2);
		CGContextFillPath(ctx);
        
		
		//UIFont *weekfont=[UIFont boldSystemFontOfSize:12];
		//NSString *date=[[[NSString alloc] initWithFormat:@"%2d",today.day] autorelease];
        NSString *date=@"今天";
        CGContextSetRGBFillColor(ctx, 130.0/255, 0, 30.0/255, 1);
		[date drawAtPoint:CGPointMake(x*swidth+15,y*itemHeight+headHeight) withFont:[UIFont boldSystemFontOfSize:16]];
        CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
        NSString* strDate=[NSString stringWithFormat:@"%04d%02d%02d",currentMonthDate.year,currentMonthDate.month,today.day];
        
        NSString* scheduleTypeStr = [MyClass getInstance].scheduleTypeStr;
        static int once=0;
        if ([MyClass getInstance].touchPoint.x==0 &&[MyClass getInstance].touchPoint.y==0 && once ==0) {
            [MyClass getInstance].touchPoint = CGPointMake(x*swidth+11+2,y*itemHeight+headHeight+12);
            once = 1;
        }
        NSArray* arr;
        arr = [self getDayFlag:strDate];
		if([arr count] > 0)
		{
            BOOL isPink=NO;
            BOOL isBlue=NO;
            BOOL isBrown=NO;
            BOOL isOrange=NO;
            BOOL isGreen=NO;
            NSInteger drawCount=0;
            for(NSDictionary* dict in arr) {
                NSString * type = [dict objectForKey:@"type"];    
                if ([MyClass getInstance].scheduleType == EScheduleType_all) {
                    if ([type isEqualToString:@"plan"] && !isPink)
                    {
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        isPink = YES;
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"deed"] && !isBlue)
                    {
                        isBlue=YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"birthday"]&& !isGreen)
                    {
                        isGreen =YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];  
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"payday"]&& !isOrange)
                    {
                        isOrange= YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"activity"]&& !isBrown)
                    {
                        isBrown= YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                }
                else
                {
                    if ([type isEqualToString:scheduleTypeStr]) {
                        if ([type isEqualToString:@"plan"] && !isPink)
                        {
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            isPink = YES;
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"deed"] && !isBlue)
                        {
                            isBlue=YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"birthday"]&& !isGreen)
                        {
                            isGreen =YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];  
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"payday"]&& !isOrange)
                        {
                            isOrange= YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"activity"]&& !isBrown)
                        {
                            isBrown= YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                    }
                }                
                
            }
		}        		
	}
}
- (void) drawCurrentSelectDate{
	int x;
	int y;
	int day;
	int todayFlag;
	if(currentSelectDate.year!=0)
	{
		CFGregorianDate today=CFAbsoluteTimeGetGregorianDate(currentTime, CFTimeZoneCopyDefault());
        
		if(today.year==currentSelectDate.year && today.month==currentSelectDate.month && today.day==currentSelectDate.day)
			todayFlag=1;
		else
			todayFlag=0;
		
		int width=self.frame.size.width;
		int swidth=width/7;
		int weekday=[self getMonthWeekday:currentMonthDate];
		day=currentSelectDate.day+weekday-2;
		x=day%7;
		y=day/7;
		CGContextRef ctx=UIGraphicsGetCurrentContext();
		
		if(todayFlag==1)
			CGContextSetRGBFillColor(ctx, 0, 0, 0.7, 1);
		else
			CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
		CGContextMoveToPoint(ctx, x*swidth+1, y*itemHeight+headHeight);
		CGContextAddLineToPoint(ctx, x*swidth+swidth+2, y*itemHeight+headHeight);
		CGContextAddLineToPoint(ctx, x*swidth+swidth+2, y*itemHeight+headHeight+itemHeight);
		CGContextAddLineToPoint(ctx, x*swidth+1, y*itemHeight+headHeight+itemHeight);
		CGContextFillPath(ctx);	
		NSString *date;
        UIFont *weekfont =[UIFont boldSystemFontOfSize:12];
		if(todayFlag==1)
		{
			CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
			CGContextMoveToPoint	(ctx, x*swidth+4,			y*itemHeight+headHeight+3);
			CGContextAddLineToPoint	(ctx, x*swidth+swidth-1,	y*itemHeight+headHeight+3);
			CGContextAddLineToPoint	(ctx, x*swidth+swidth-1,	y*itemHeight+headHeight+itemHeight-3);
			CGContextAddLineToPoint	(ctx, x*swidth+4,			y*itemHeight+headHeight+itemHeight-3);
			CGContextFillPath(ctx);	
            date=@"今天";
            CGContextSetRGBFillColor(ctx, 130.0/255, 0, 30/255, 1);
            [date drawAtPoint:CGPointMake(x*swidth+15,y*itemHeight+headHeight) withFont:[UIFont boldSystemFontOfSize:16]];
            CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
		}
		else
        {
            date = [[[NSString alloc] initWithFormat:@"%2d",currentSelectDate.day] autorelease];
            CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
            [date drawAtPoint:CGPointMake(x*swidth+25,y*itemHeight+headHeight) withFont:weekfont];
        }
        
        NSString* strDate=[NSString stringWithFormat:@"%04d%02d%02d",currentMonthDate.year,currentMonthDate.month,currentSelectDate.day];
        
        NSString* scheduleTypeStr = [MyClass getInstance].scheduleTypeStr;

        NSArray* arr;
        arr = [self getDayFlag:strDate];
		if([arr count] > 0)
		{
            BOOL isPink=NO;
            BOOL isBlue=NO;
            BOOL isBrown=NO;
            BOOL isOrange=NO;
            BOOL isGreen=NO;
            NSInteger drawCount=0;
            for(NSDictionary* dict in arr) {
                NSString * type = [dict objectForKey:@"type"];    
                if ([MyClass getInstance].scheduleType == EScheduleType_all)
                {
                    if ([type isEqualToString:@"plan"] && !isPink)
                    {
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        isPink = YES;
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"deed"] && !isBlue)
                    {
                        isBlue=YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"birthday"]&& !isGreen)
                    {
                        isGreen =YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];  
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"payday"]&& !isOrange)
                    {
                        isOrange= YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                    else if ([type isEqualToString:@"activity"]&& !isBrown)
                    {
                        isBrown= YES;
                        if (drawCount>=4) {
                            [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                        }
                        else
                            [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                        drawCount++;
                    }
                }
                else
                {
                    if ([type isEqualToString:scheduleTypeStr]) {
                        if ([type isEqualToString:@"plan"] && !isPink)
                        {
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"pink.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            isPink = YES;
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"deed"] && !isBlue)
                        {
                            isBlue=YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"blue.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"birthday"]&& !isGreen)
                        {
                            isGreen =YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"green.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];  
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"payday"]&& !isOrange)
                        {
                            isOrange= YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"orange.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                        else if ([type isEqualToString:@"activity"]&& !isBrown)
                        {
                            isBrown= YES;
                            if (drawCount>=4) {
                                [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+12)];
                            }
                            else
                                [[UIImage imageNamed:@"brown.png"] drawAtPoint:CGPointMake(x*swidth+drawCount*11+2,y*itemHeight+headHeight+22)];
                            drawCount++;
                        }
                    }
                }
            }
		}        
	
	}
}
- (void) touchAtDate:(CGPoint) touchPoint{
    [MyClass getInstance].touchPoint = touchPoint;
	int x;
	int y;
	int width=self.frame.size.width;
	int weekday=[self getMonthWeekday:currentMonthDate];
	int monthDayCount=[self getDayCountOfaMonth:currentMonthDate];
	x=touchPoint.x*7/width;
	y=(touchPoint.y-headHeight)/itemHeight;
	int monthday=x+y*7-weekday+2;
	if(monthday>0 && monthday<monthDayCount+1)
	{
		currentSelectDate.year=currentMonthDate.year;
		currentSelectDate.month=currentMonthDate.month;
		currentSelectDate.day=monthday;
		currentSelectDate.hour=0;
		currentSelectDate.minute=0;
		currentSelectDate.second=1;
        [MyClass getInstance].selectDate = currentSelectDate;
		[calendarViewDelegate selectDateChanged:currentSelectDate];
		[self setNeedsDisplay];
	}
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch* touch=[touches anyObject];
	CGPoint touchPoint=[touch locationInView:self];
    if(touchPoint.y>headHeight)
	{
		[self touchAtDate:touchPoint];
	}
}

- (void)drawRect:(CGRect)rect{
    
	static int once=0;
	currentTime=CFAbsoluteTimeGetCurrent();
	[self drawTopBarWords];
	[self drawGirdLines];
	
	if(once==0)
	{
		once=1;
		float height;
		int row_Count=([self getDayCountOfaMonth:currentMonthDate]+[self getMonthWeekday:currentMonthDate]-2)/7+1;
		height=row_Count*itemHeight+headHeight;
		[calendarViewDelegate monthChanged:currentMonthDate viewLeftTop:self.frame.origin height:height];
		[calendarViewDelegate beforeMonthChange:self willto:currentMonthDate];
        [calendarViewDelegate initDisplayToday];
	}
	[self drawDateWords];
	[self drawToday];
	[self drawCurrentSelectDate];
	
}
-(void)goTaday{
    NSInteger intFlag=0;
    CFGregorianDate currentDate;
    currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(),CFTimeZoneCopyDefault());
    if (currentDate.year > currentMonthDate.year) {
        intFlag = 1;
    }
    else if (currentDate.year == currentMonthDate.year){
        if (currentDate.month > currentMonthDate.month) {
            intFlag=1;
        }
        else if (currentDate.month == currentMonthDate.month){
            return;    
        }
        else{
            intFlag=0;
        }
    }
    else{
        intFlag=0;
    }    
    currentMonthDate = currentDate;
    [self movePrevNext:intFlag];
}
-(NSString*)getStrDate{
    NSString* strDate;
    strDate = [NSString stringWithFormat:@"%4d年%2d月",currentMonthDate.year,currentMonthDate.month];
    return strDate;
}
- (void)dealloc {
	self.monthFlagArray=nil;
    [super dealloc];
}


@end
